package cn.yusei.teamTribe.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yusei.teamTribe.constant.PermissionType;
import cn.yusei.teamTribe.constant.PostTagType;
import cn.yusei.teamTribe.dao.PermissionDAO;
import cn.yusei.teamTribe.dao.PermissionPO;
import cn.yusei.teamTribe.dao.post.PostReflecDAO;
import cn.yusei.teamTribe.dao.post.PostTagDAO;
import cn.yusei.teamTribe.dao.post.po.PostTagPO;
import cn.yusei.teamTribe.dao.school.SchoolRoleDAO;
import cn.yusei.teamTribe.dao.school.po.SchoolRolePO;
import cn.yusei.teamTribe.dao.team.TeamInfoDAO;
import cn.yusei.teamTribe.dao.team.TeamRoleDAO;
import cn.yusei.teamTribe.dao.team.po.TeamRolePO;

@Service
public class PermissionService {

	@Autowired
	private TeamRoleDAO teamRoleDAO;
	@Autowired
	private PermissionDAO permissionDAO;
	@Autowired
	private TeamInfoDAO teamInfoDAO;
	@Autowired
	private SchoolRoleDAO schoolRoleDAO;
	@Autowired
	private PostReflecDAO postReflecDAO;
	@Autowired
	private PostTagDAO postTagDAO;
	
	/**
	 * 
	 * @param userId
	 * @param teamId
	 * @param permissionId
	 * @param type 0 代表 teamId 的位置为 teamId，1代表schoolId
	 * @return
	 */
	public boolean havePermission(int userId, int teamId, int permissionId,int type)
	{
		if(type == 0){
			
		switch (permissionId)
		{
			case PermissionType.SET_TEAM_MASTER:
				return haveSchoolPermission(userId, teamId, permissionId);
			case PermissionType.SET_TEAM_MANAGER:
			case PermissionType.SET_TEAM_INFORM_POST:
			case PermissionType.SET_TEAM_NORMAL_POST:
			case PermissionType.UPDATE_TEAM_INFO:
			case PermissionType.TEAM_MEMBER_MANAGE:
				return haveTeamPermission(userId, teamId, permissionId);
			default: throw new IllegalArgumentException("权限 [" + permissionId + "] 不存在");
		}
		}else if(type == 1){
			return haveSchoolPermission2(userId, teamId, permissionId);
		}
		return false;
	}
	
	private boolean haveSchoolPermission2(int userId,int schoolId,int permissionId){
		SchoolRolePO schoolRolePO = new SchoolRolePO();
		schoolRolePO.setUser_id(userId);
		schoolRolePO.setSchool_id(schoolId);
		Integer roleId = schoolRoleDAO.selectRole(schoolRolePO);
		if(roleId == null){
			return false;
		}
		PermissionPO permissionPO = new PermissionPO();
		permissionPO.setPermissio_id(permissionId);
		permissionPO.setRoleId(roleId);
		return permissionDAO.exists(permissionPO);
					
	}
	
	private boolean haveSchoolPermission(int userId,int teamId, int permissionId)
	{
		Integer schoolId = teamInfoDAO.selectSchoolId(teamId);
		 //Team 不存在
		if(schoolId == null)
			return false;
		SchoolRolePO schoolRolePO = new SchoolRolePO();
		schoolRolePO.setSchoolId(schoolId);
		schoolRolePO.setUserId(userId);
		Integer roleId = schoolRoleDAO.selectRole(schoolRolePO);
		if(roleId == null)
			return false;
		PermissionPO permissionPO = new PermissionPO();
		permissionPO.setPermissionId(permissionId);
		permissionPO.setRoleId(roleId);
		return permissionDAO.exists(permissionPO);
	}
	
	private boolean haveTeamPermission(int userId, int teamId, int permissionId)
	{
		TeamRolePO teamRolePO = new TeamRolePO();
		teamRolePO.setUserId(userId);
		teamRolePO.setTeamId(teamId);
		Integer roleId = teamRoleDAO.selectRole(teamRolePO);
		if(roleId == null)
			return false;
		PermissionPO permissionPO = new PermissionPO();
		permissionPO.setPermissionId(permissionId);
		permissionPO.setRoleId(roleId.intValue());
		return permissionDAO.exists(permissionPO); 
	}
	
	private boolean havePostPermission(int userId, int postId, int permissionId)
	{
		Integer teamId = postReflecDAO.selectTeamById(postId);
		if(teamId == null)
			return false;
		return havePermission(userId, teamId.intValue(), permissionId, 0);
	}
	
	/**
	 * 
	 * @param userId
	 * @param postId
	 * @param informPermission  强制需要发通知帖的权利（用于把帖子设置为通知帖时）
	 * @return
	 */
	public boolean havePostPermission(int userId, int postId, boolean informPermission)
	{
		PostTagPO postTagPO = new PostTagPO();
		postTagPO.setPostId(postId);
		postTagPO.setTagId(PostTagType.INFORM_POST);
		boolean isInformPost = informPermission || postTagDAO.exists(postTagPO);
		return havePostPermission(userId, postId,
				isInformPost ? PermissionType.SET_TEAM_INFORM_POST : PermissionType.SET_TEAM_NORMAL_POST);
	}
	
}
